package synchronize;

public class NotifyDemo {

    public synchronized void testWait() {
        System.out.println(Thread.currentThread().getName() + " Start-----");
        try {
            wait(0);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " End-------");
    }

    public static void main(String[] args) throws InterruptedException {
        final NotifyDemo notifyDemo = new NotifyDemo();
        for (int i = 0; i < 5; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    notifyDemo.testWait();
                }
            }).start();
        }

        Thread.sleep(1000);

        //只通过锁对象唤醒一个线程
        System.out.println("notify");
//        Exception in thread "main" notify
//        java.lang.IllegalMonitorStateException
//        at java.lang.Object.notify(Native Method)
        notifyDemo.notify();

        Thread.sleep(1000);

        //通过锁对象唤醒所有线程
        System.out.println("notify all");
        notifyDemo.notifyAll();
    }

}
